import path from "node:path";
import fs from "node:fs";
import { CmdParams } from "../task/cmd/cmd.params";

/**
 * 获取项目目录
 * @param dirPath
 * @param fileName
 */
export const getProjectDir = (dirPath: string = "", fileName = "") => {
  let p = path.join(__dirname, "../../", dirPath);
  if (CmdParams.params.home) {
    p = path.join(CmdParams.params.home, dirPath);
  }
  if (!fs.existsSync(p)) fs.mkdirSync(p, { recursive: true });
  return path.join(p, fileName);
};

/**
 * 获取 app 数据目录
 * 1. 在运行时，会自动创建该目录
 * 2. 目录位置位于server.js 的同一级目录中的 .data 文件夹
 */
export const getAppDataPath = (dirPath: string = "", fileName = "") => {
  return getProjectDir(path.join(".data", dirPath), fileName);
};
